package holiday_demo.override_toStringAndEquals;

import java.util.Objects;

public class student {
    private String name;
    private int age;

    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        student student = (student) o;
        return age == student.age && Objects.equals(name, student.name);
    }


    public static void main(String[] args) {
        //未重写toString方法显示的结果为：holiday_demo.override_toStringAndEquals.student@4554617c
        student stu=new student("小明",17);
        System.out.println(stu);
        //重写后：student{name='小明', age=17}

        /*---equals方法--------------*/
        student stu2=new student("小明",17);
        //未重写equals方法，利用默认equals方法来比较两个对象间的值
        /*参考官方文档可以看到，默认equals方法比较的是两个对象的地址是否相等
        public boolean equals(Object obj) {
        return (this == obj);
        "=="比较的对象是基本数据类型是，是比较二者的值；但面对引用数据类型是，比较的是二者的地址
        }
         */
        //System.out.println("比较结果为："+stu.equals(stu2));
        //比较结果为：false


        //重写equals方法后
        System.out.println("比较结果为："+stu.equals(stu2));
        //结果为true

    }
}
